package cn.wu.reggie.controller;

import cn.wu.reggie.common.R;
import cn.wu.reggie.entity.Category;
import cn.wu.reggie.query.CategoryQuery;
import cn.wu.reggie.service.ICategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;


    // 对应不上前端的page数据 用注解
    @GetMapping("page")
    public R findByPage(CategoryQuery qo){
        // 调用service完成分页查询操作
        Page<Category> page = categoryService.queryPage(qo);
        return R.success(page);
    }

    @PostMapping
    public R save(@RequestBody Category category, HttpSession session){
        // 输入category封装的用户id
        Long userId = (Long) session.getAttribute("employee");
        category.setUpdateUser(userId);
        category.setCreateUser(userId);

        // 调用service完成保存
        return categoryService.saveOne(category);

    }

    @PutMapping
    public R update(@RequestBody Category category, HttpSession session){
        // 给category封装更新用户id
        Long userId = (Long) session.getAttribute("employee");
        category.setUpdateUser(userId);

        // 调用service完成更新操作
        return  categoryService.updateOne(category);
    }

    @DeleteMapping
    public R delete(Long id){
        return categoryService.delete(id);
    }

    @GetMapping("list")
    public R list(Integer type){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }

}